package com.vince.servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.fabric.xmlrpc.base.Array;
import com.vince.model.domain.User;
import com.vince.model.service.Paging;
import com.vince.model.service.UserService;
import com.vince.util.DBUtil;

/**
 *  前台提供curPage变更的参数 
 * @author susp
 *
 */
@WebServlet("/page")
public class PageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//
		String curpage=request.getParameter("curpage");
		String num=request.getParameter("num");
		Collection data= UserService.getData();//获取所有用户记录
		Paging page=new Paging(data, 4);
		//根据前台修改curPage
		if("first".equals(curpage)){
			page.setFirst();
		}else if("last".equals(curpage)){
			page.setLast();
		}else if("previous".equals(curpage)){
			page.previous();
			
		}else if("next".equals(curpage)){
			page.next();
		}
		if(num!=null){
			page.setCurPage(Integer.valueOf(num));
		}
		System.out.println("当前页是："+page.getCurPage());
		Collection userlist=page.getPageData();
		request.setAttribute("userlist", userlist);//默认是第一页
		request.setAttribute("pageNow", page.getCurPage());
		request.setAttribute("pageCount", page.getPageCount());
		//刷新用户管理页面
		request.getRequestDispatcher("/view/user/usermanage.jsp").forward(request, response);
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
